package com.rc.evcharger.controller;

import com.rc.evcharger.common.base.TResponse;
import com.rc.evcharger.model.elec.ElecPile;
import com.rc.evcharger.protocol.ylevcharger.service.YlCommandService;
import com.rc.evcharger.service.elec.ElecPileService;
import com.rc.evcharger.service.elec.ElecStationService;
import com.rc.evcharger.service.rc.RcSettingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author yang
 */
@RestController
@RequestMapping("/cmdTenant")
public class TenantController {
    Logger logger = LoggerFactory.getLogger(TenantController.class);

    @Autowired
    protected ElecStationService elecStationService;

    @Autowired
    private YlCommandService ylCommandService;

    @Autowired
    private ElecPileService elecPileService;


    /**
     * 整型参数设置
     */
    @ResponseBody
    @RequestMapping(value = "/hearBeatSetting/{pileNo}", method = RequestMethod.POST)
    public TResponse hearBeatSetting(@PathVariable("pileNo") String pileNo, Integer sendType) {
        logger.info("下发心跳间隔时间(整型参数设置),对应的桩编号：{}，类型:{}", pileNo, sendType);
        if (pileNo == null) {
            return TResponse.FAIL("桩编号不能为空");
        }

        if (sendType == null){
            sendType = 0;
        }

        ElecPile elecPile = elecPileService.selectByPileNo(pileNo);

        if (elecPile == null) {
            return TResponse.FAIL("找不到对应桩,桩编号:" + pileNo);
        }

        try {
            ylCommandService.sendIntegerParam(elecPile.getElecPileId(), sendType);
            //推送订单停止状态,推送类型 0正常充电 1后台补推送 默认0
        } catch (Exception e) {
            logger.error("下发心跳间隔时间(整型参数设置),桩编号:{}", pileNo, e);
            TResponse.FAIL("下发心跳间隔时间(整型参数设置),桩编号:" + pileNo);
        }

        TResponse tResponse = TResponse.SUCCESS();
        tResponse.put("下发心跳间隔时间(整型参数设置)" + pileNo);
        return tResponse;
    }


    /**
     * 设备重启
     */
    @ResponseBody
    @RequestMapping(value = "/resetPile/{pileNo}", method = RequestMethod.POST)
    public TResponse resetPile(@PathVariable("pileNo") String pileNo) {
        logger.info("设备重启,对应的桩编号：{}，类型:{}", pileNo);
        if (pileNo == null) {
            return TResponse.FAIL("桩编号不能为空");
        }

        ElecPile elecPile = elecPileService.selectByPileNo(pileNo);
        if (elecPile == null) {
            return TResponse.FAIL("找不到对应桩,桩编号:{}" + pileNo);
        }

        try {
            ylCommandService.sendResetPile(elecPile.getElecPileId());
            //推送订单停止状态,推送类型 0正常充电 1后台补推送 默认0
        } catch (Exception e) {
            logger.error("下发心跳间隔时间(整型参数设置),桩编号:{}", pileNo, e);
            TResponse.FAIL("下发心跳间隔时间(整型参数设置),桩编号:" + pileNo);
        }

        TResponse tResponse = TResponse.SUCCESS();
        tResponse.put("下发心跳间隔时间(整型参数设置)" + pileNo);
        return tResponse;
    }



    /**
     * 下发电价
     */
    @ResponseBody
    @RequestMapping(value = "/sendPrice/{pileNo}", method = RequestMethod.POST)
    public TResponse sendPrice(@PathVariable("pileNo") String pileNo) {
        logger.info("下发电价,对应的桩编号：{}，类型:{}", pileNo);
        if (pileNo == null) {
            return TResponse.FAIL("桩编号不能为空");
        }

        ElecPile elecPile = elecPileService.selectByPileNo(pileNo);
        if (elecPile == null) {
            return TResponse.FAIL("找不到对应桩,桩编号:" + pileNo);
        }

        try {
            ylCommandService.sendPrice(elecPile.getElecPileId());
            //推送订单停止状态,推送类型 0正常充电 1后台补推送 默认0
        } catch (Exception e) {
            logger.error("下发电价,桩编号:{}", pileNo, e);
            TResponse.FAIL("下发电价,桩编号:" + pileNo);
        }

        TResponse tResponse = TResponse.SUCCESS();
        tResponse.put("下发电价" + pileNo);
        return tResponse;
    }
}
